package com.ruoyi.business.controller;

import java.math.BigDecimal;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.lang.UUID;
import com.ruoyi.business.domain.TOrderFrom;
import com.ruoyi.business.service.ITOrderFromService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.TSku;
import com.ruoyi.business.service.ITSkuService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品管理Controller
 * 
 * @author ruoyi
 * @date 2024-06-13
 */
@RestController
@RequestMapping("/business/sku")
public class TSkuController extends BaseController
{
    @Autowired
    private ITSkuService tSkuService;

    /**
     * 查询商品管理列表
     */

    @GetMapping("/list")
    public TableDataInfo list(TSku tSku)
    {
        startPage();
        List<TSku> list = tSkuService.selectTSkuList(tSku);
        return getDataTable(list);
    }

    /**
     * 导出商品管理列表
     */

    @Log(title = "商品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TSku tSku)
    {
        List<TSku> list = tSkuService.selectTSkuList(tSku);
        ExcelUtil<TSku> util = new ExcelUtil<TSku>(TSku.class);
        util.exportExcel(response, list, "商品管理数据");
    }

    /**
     * 获取商品管理详细信息
     */

    @GetMapping(value = "/{skuId}")
    public AjaxResult getInfo(@PathVariable("skuId") Integer skuId)
    {
        return success(tSkuService.selectTSkuBySkuId(skuId));
    }

    /**
     * 新增商品管理
     */

    @Log(title = "商品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TSku tSku)
    {
        return toAjax(tSkuService.insertTSku(tSku));
    }

    /**
     * 修改商品管理
     */

    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TSku tSku)
    {
        return toAjax(tSkuService.updateTSku(tSku));
    }

    /**
     * 删除商品管理
     */
    @Log(title = "商品管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{skuIds}")
    public AjaxResult remove(@PathVariable Integer[] skuIds)
    {
        return toAjax(tSkuService.deleteTSkuBySkuIds(skuIds));
    }


    /**
     * 下单
     */
   @Resource
   private ITOrderFromService itOrderFromService;
    @PostMapping("/order")
    public AjaxResult createOrder(@RequestBody TSku tSku)
    {
        // 数据处理
        Long numL = Long.valueOf(tSku.getNum());
        Long skuStock = tSku.getSkuStock();
        // 校验
        if (skuStock == null || skuStock == 0) {
            return AjaxResult.warn("库存不足");
        }
        TOrderFrom tOrderFrom = gettOrderFrom(tSku);

        // 下单
        itOrderFromService.createOrder(tOrderFrom);

        tSku.setSkuStock(tSku.getSkuStock() - numL);
        tSkuService.updateTSku(tSku);
        return AjaxResult.success("下单成功");
    }


    public   static  final  Long unPaid =  1L;
    public   static  final  Long Paid =  2L;
    @NotNull
    public static TOrderFrom gettOrderFrom(TSku tSku) {
        TOrderFrom tOrderFrom = new TOrderFrom();
        // 订单属性
        tOrderFrom.setType(1L);
        tOrderFrom.setGrade(2L);
        tOrderFrom.setStatus(unPaid);
        // uuid
        String uuid = UUID.randomUUID().toString().replace("-", "");
        tOrderFrom.setNo(tSku.getSkuName()+"-"+uuid);
        /// sku信息
        tOrderFrom.setSkuId(Long.valueOf(tSku.getSkuId()));// id
        tOrderFrom.setSkuName(tSku.getSkuName());// name
        tOrderFrom.setSkuNum(tSku.getNum());// num
        // 订单价格
        BigDecimal skuPrice = tSku.getSkuPrice();
        String num = tSku.getNum();
        tOrderFrom.setPrice(skuPrice.multiply(new BigDecimal(num)));
        // 用户信息
        tOrderFrom.setUserId(tSku.getUserId());
        return tOrderFrom;
    }
}
